<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>员工信息管理系统 - 登录</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
        }

        .login-container {
            background-color: #fff;
            border-radius: 10px;
            box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
            padding: 40px;
            width: 400px;
        }

        .login-header {
            text-align: center;
            margin-bottom: 30px;
        }

        .login-header h1 {
            color: #667eea;
            font-size: 28px;
            margin-bottom: 10px;
        }

        .login-header p {
            color: #999;
            font-size: 14px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #333;
            font-size: 14px;
        }

        .form-group input {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 14px;
            transition: border-color 0.3s;
        }

        .form-group input:focus {
            outline: none;
            border-color: #667eea;
        }

        .btn-login {
            width: 100%;
            padding: 12px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: #fff;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            cursor: pointer;
            transition: transform 0.3s;
        }

        .btn-login:hover {
            transform: translateY(-2px);
        }

        .btn-login:active {
            transform: translateY(0);
        }

        .error-message {
            color: #f56c6c;
            font-size: 12px;
            margin-top: 5px;
            display: none;
        }

        .error-message.show {
            display: block;
        }

        .login-footer {
            text-align: center;
            margin-top: 20px;
            color: #999;
            font-size: 12px;
        }

        .default-info {
            background-color: #f0f9ff;
            border: 1px solid #409eff;
            border-radius: 5px;
            padding: 10px;
            margin-bottom: 20px;
            font-size: 12px;
            color: #409eff;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="login-header">
            <h1>员工信息管理系统</h1>
            <p>Employee Management System</p>
        </div>

        <div class="default-info">
            默认账号：admin / admin123
        </div>

        <form id="loginForm">
            <div class="form-group">
                <label>用户名</label>
                <input type="text" id="username" name="username" placeholder="请输入用户名" required>
            </div>

            <div class="form-group">
                <label>密码</label>
                <input type="password" id="password" name="password" placeholder="请输入密码" required>
            </div>

            <div class="error-message" id="errorMessage"></div>

            <button type="submit" class="btn-login">登 录</button>
        </form>

        <div class="login-footer">
            © 2024 员工信息管理系统
        </div>
    </div>

    <script>
        document.getElementById('loginForm').addEventListener('submit', function(e) {
            e.preventDefault();

            const username = document.getElementById('username').value.trim();
            const password = document.getElementById('password').value.trim();
            const errorMessage = document.getElementById('errorMessage');

            // 验证
            if (!username) {
                errorMessage.textContent = '请输入用户名';
                errorMessage.classList.add('show');
                return;
            }
            if (!password) {
                errorMessage.textContent = '请输入密码';
                errorMessage.classList.add('show');
                return;
            }

            errorMessage.classList.remove('show');

            // 构建表单数据
            const formData = new URLSearchParams();
            formData.append('username', username);
            formData.append('password', password);

            // 提交登录请求
            fetch('login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                body: formData.toString()
            })
            .then(response => response.json())
            .then(data => {
                if (data.code === 200) {
                    // 登录成功，保存用户名到sessionStorage
                    sessionStorage.setItem('username', username);
                    // 跳转到员工管理页
                    window.location.href = 'pages/employee.html';
                } else {
                    // 登录失败，显示错误信息
                    errorMessage.textContent = data.message;
                    errorMessage.classList.add('show');
                }
            })
            .catch(error => {
                console.error('登录失败:', error);
                errorMessage.textContent = '登录失败，请稍后重试';
                errorMessage.classList.add('show');
            });
        });

        // 清除错误信息
        document.querySelectorAll('input').forEach(input => {
            input.addEventListener('input', function() {
                document.getElementById('errorMessage').classList.remove('show');
            });
        });
    </script>
</body>
</html>
